package com.project.easy.common.log.util;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2022/3/20 18:24
 * @description:
 */
public final class LogBackTool {
    /**
     * 私有构造方法
     */
    private LogBackTool() {
    }
    /**
     * 动态日志刷新
     * @param loggerPackage 日志包名称
     * @param loggerLevel 日志等级
     */
    public static void refreshLevel(String loggerPackage, Level loggerLevel) {
        // #1.get logger context
        ch.qos.logback.classic.LoggerContext loggerContext = (ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
        List<Logger> loggerList = loggerContext.getLoggerList();
        // #2.filter the Logger object
        List<Logger> packageLoggerList = loggerList.stream().filter(a -> a.getName().startsWith(loggerPackage)).collect(Collectors.toList());
        // #3.set level to logger
        for (ch.qos.logback.classic.Logger logger : packageLoggerList) {
            logger.setLevel(loggerLevel);
        }
    }

}
